package com.sd365.permission.centre.api.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.RoleApi;
import com.sd365.permission.centre.entity.Node;
import com.sd365.permission.centre.entity.Role;
import com.sd365.permission.centre.pojo.dto.*;
import com.sd365.permission.centre.pojo.query.RoleQuery;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.*;
import com.sd365.permission.centre.service.RoleService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @program: sd365-permission-centre
 * @description: 角色管理Controller实现类  ，实现RoleApi接口实现界面接口所定义的方法，要求满足
 * 功能需求
 * @author: Mr.Li
 * @create: 2020-12-11 17:03
 **/
@CrossOrigin
@RestController
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
public class RoleController extends AbstractController implements RoleApi {
    @Resource
    RoleService roleService;
    @Override
    public Boolean initRoleResourceRedis(Role role) {
        return roleService.initRoleResourceRedis(role);
    }

    @Override
    public Boolean assignResource(RoleResourceDTO roleResourceDTO) {
        return roleService.assignResource(roleResourceDTO);
    }

    @Override
    public Boolean assignUser(UserRoleDTO userRoleDTO) {
        return roleService.assignUser(userRoleDTO);
    }

    @Override
    public List<Node> doQueryResourceByRoleId(RoleQuery roleQuery) {
        return roleService.queryResourceByRoleId(roleQuery.getId());
    }

    @Override
    public List<Node> selectCompany() {
        return roleService.selectCompany();
    }

    @Override
    public boolean haveRole(RoleDTO roleDTO) {
        return roleService.haveRole(roleDTO);
    }

    @Override
    public Boolean add(RoleDTO roleDTO) {
        return roleService.add(roleDTO);
    }

    @Override
    public Boolean remove(Long id, Long version) {
        return roleService.remove(id, version);
    }

    @Override
    public Boolean batchRemove(@Valid RoleDTO[] roleDTOS) {
        return roleService.batchRemove(roleDTOS);
    }

    @Override
    public Boolean modify(RoleDTO roleDTO) {
        RoleDTO roleDTO1=roleService.modify(roleDTO);
        return (roleDTO1.equals(roleDTO));
    }

    @Override
    public RoleDTO copy(Long id) {
        return roleService.copy(id);
    }

    @Override
    public List<RoleVO> commonQuery(RoleQuery roleQuery) {
        return BeanUtil.copyList(roleService.commonQuery(roleQuery), RoleVO.class, new BeanUtil.CopyCallback() {
            @Override
            public void copy(Object o, Object o1) {
              RoleDTO  roleDTODTO = (RoleDTO)  o;
              RoleVO roleVO = (RoleVO) o1;
                  if(roleDTODTO.getResourceDTOS()!=null){
                      roleVO.setResourceVOS(BeanUtil.copyList(roleDTODTO.getResourceDTOS(),ResourceVO.class));
                  }
            }
        });
    }

    @Override
    public List<UserVO> commonQueryUser(UserQuery userQuery) {
        return BeanUtil.copyList(roleService.commonQueryUser(userQuery), UserVO.class, new BeanUtil.CopyCallback() {
            @Override
            public void copy(Object o, Object o1) {
                UserDTO userDTO=(UserDTO) o;
                UserVO userVO=(UserVO) o1;
                if(userDTO.getCompany()!=null){
                    userVO.setCompany(BeanUtil.copy(userDTO.getCompany(),CompanyVO.class));
                }
                if(userDTO.getDepartment()!=null){
                    userVO.setDepartmentVO(BeanUtil.copy(userDTO.getDepartment(),DepartmentVO.class));
                }
                if(userDTO.getOrganization()!=null){
                    userVO.setOrganization(BeanUtil.copy(userDTO.getCompany(), OrganizationVO.class));
                }
                if(userDTO.getPosition()!=null){
                    userVO.setPositionVO((BeanUtil.copy(userDTO.getPosition(), PositionVO.class)));
                }
            }
        });
    }

    @Override
    public RoleVO queryRoleById(Long id) {
        RoleDTO roleDTO=roleService.queryById(id);
        RoleVO roleVO= BeanUtil.copy( roleService.queryById(id),RoleVO.class);
        if(roleDTO.getResourceDTOS()!=null){
            roleVO.setResourceVOS(BeanUtil.copyList(roleDTO.getResourceDTOS(),ResourceVO.class));
        }
        return roleVO;
    }

    @Override
    public RoleVO queryUserResource(Long id) {
        return BeanUtil.copy( roleService.queryResourceByRoleId(id),RoleVO.class);
    }

    @Override
    public RoleCompanyVO queryRoleCompanyById(Long id) {
        return  BeanUtil.copy( roleService.queryRoleCompanyById(id),RoleCompanyVO.class);
    }

    @Override
    public Boolean modifyRoleCompany(RoleCompanyDTO roleCompanyDTO) {
        return roleService.modifyRoleCompany(roleCompanyDTO);
    }
}
